<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>组件之间通信</title>
</head>
<body>
<div id="app">
    <my-div message="今天很好" url="../../static/favicon.ico"></my-div>
    <my-div message="今天不好" url="../../static/favicon.ico"></my-div>
    <h2>多层组件传递，一定要用动态绑定</h2>
    <my-parent :title="title" :url="img"></my-parent>

    <p>父组件监听子组件的事件获取值，{{totalCounter}}</p>

    <search @total="allCount()"></search>
    <search @total="allCount()"></search>
    <search @total="allCount()"></search>
    <search @total="allCount()"></search>
    <search @total="allCount()"></search>

    <p>父组件与子组件通过自定义事件进行，即自定义函数的方式返回{{totalCounter}}</p>
</div>

<template id="my-div">
    <div>
        <h3>天气不错{{message}}</h3>
        <img :src="url">
    </div>
</template>

<template id="my-img">
    <img v-bind:src="url"></img>
</template>

<template id="my-title">
    <p>{{title}}</p>
</template>

<!-- 多层传递，一定要用动态绑定-->
<template id="my-parent">
    <div>
        <my-title :title="title"></my-title>
        <my-img :url="url"></my-img>
    </div>
</template>

<template id="search">
        <button @click="total()">点击了{{count}}次</button>
</template>
</body>
<script src="../../node_modules/vue/dist/vue.js"></script>
<script>

    // 组件间通信，变量不支持驼峰命名法
    Vue.component('my-div', {
        template: '#my-div',
        // 定义组件之间传递信息的方式,定义变量
        props: ['message', 'url']
    })

    let child1 = Vue.extend({
        template: '#my-img',
        props: ['url']
    });

    let child2 = Vue.extend({
        template: '#my-title',
        props: ['title']
    });

    // 子组件自定义事件
    Vue.component('search', {
        template: '#search',
        data() {
            return {
                count: 0
            }
        },
        methods: {
            total() {
                this.count += 1;
                // 通知外界调用了这个方法
                this.$emit('total')
            }
        }
    })

    Vue.component('my-parent', {
        props: ['title', 'url'],
        components: {
            'my-title': child2,
            'my-img': child1
        },
        template: '#my-parent'
    })


    let vm = new Vue({
        el: '#app',
        data: {
            title: '你好啊',
            img: '../../static/favicon.ico',
            totalCounter: 0
        },
        methods: {
            allCount() {
                this.totalCounter += 1;
            }
        }
    });
</script>
</html>
